package com.li.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lirenchun
 */
@RefreshScope
@RestController
@RequestMapping("/test")
@EnableHystrix
public class TestController {
    @Value("${aliyun}")
    private String accessKeyId;


    @RequestMapping("/t1")
    public String test1(HttpServletRequest request) {
        System.out.println(accessKeyId);
        //打印请求的url,用于判断负载是否生效
        System.out.println(request.getRequestURL().toString());
        return "hello  world!";
    }

    /**
     * 请求超时服务降级测试
     *
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/t2")
    @HystrixCommand(fallbackMethod = "backMethod",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
            }
    )
    public String test2() throws InterruptedException {
        Thread.sleep(2000);
        return "hello  world!";
    }

    /**
     * 服务降级方法
     *
     * @return
     */
    private String backMethod() {
        return "请求失败，响应超时...";
    }
}
